// 数据请求 wx.request
const axios = require("axios")
class httpRequest {
  constructor() {

  }
  //使用axios作为服务发布的接口
  static sendRrquest(url, method, data) {

    var promise = new Promise(function (resolve, reject) {
      axios.get(url)
        .then((response) => {
          resolve(response); //返回请求体
        })
        .catch((error) => {
          resolve(error); //异常
        });
    })
    return promise

  }
  static sendRrquest1(url, method, data) {
    var status = true;
    var header = {
      'content-type': 'application/x-www-form-urlencoded'
    }

    var promise = new Promise(function (resolve, reject) {
      wx.getNetworkType({
        success: function (res) {
          // 返回网络类型2g，3g，4g，wifi, none, unknown
          var networkType = res.networkType
          if (networkType == "none") {
            wx.hideLoading();
            //没有网络连接
            wx.showModal({
              title: '提示',
              content: '网络连接失败,请检查您的网络设置',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  //返回res.confirm为true时，表示用户点击确定按钮
                  console.log('表示用户点击确定按钮')

                }
              }
            })
            status = false;
          } else if (networkType == "unknown") {
            wx.hideLoading();
            //未知的网络类型
            wx.showModal({
              title: '提示',
              content: '未知的网络类型,请检查您的网络设置',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  //返回res.confirm为true时，表示用户点击确定按钮
                  console.log('表示用户点击确定按钮')
                }
              }
            })
            status = false;
          } else {
            wx.request({
              url: url,
              data: data,
              method: method,
              header: header,
              success: resolve,
              fail: reject
            })

          }
        }
      })
      return status
    });
    return promise;
  }
}
module.exports = httpRequest
// export {
//   httpRequest
// }